<?php
require ('../include/init.inc.php');
$tb_id = $tb_date = $tb_type = $tb_provide_name = $tb_provide_id = $tb_order_id = $tb_cost_photo = $tb_remark = '';
extract ( $_POST, EXTR_IF_EXISTS );
if (Common::isPost ()) {
    
    $pic = $_FILES['tb_cost_photo']['tmp_name'];
    if($pic == "") {
        Common::exitWithError(ErrorMessage::PHOTO_ERROR,"panel/travel_bill.php");
    }
    $upload_ret = false;
    if($pic){
        // 上传的路径
        $photoDir = $_SERVER['DOCUMENT_ROOT']."/photos/travel/travelBill";
        // 创建文件夹
        if(!file_exists($photoDir)){
            mkdir($photoDir, 0777);
        }
        // 用时间戳来保存图片，防止重复
        $timeNow = time();
        $targetFile = $photoDir . '/' . $timeNow.'.jpg';
        $sqlFile = "/photos/travel/travelBill/".$timeNow.'.jpg';
        echo $targetFile."<br / >";
        // 将临时文件 移动到我们指定的路径，返回上传结果
        $upload_ret = move_uploaded_file($pic, $targetFile) ? true : false;
    }
    if($upload_ret == false){
        Common::exitWithError(ErrorMessage::PHOTO_ERROR,"panel/travel_bill.php");
    }
    
	$exist = TravelBill::getTravelBillById($tb_id);
	if($exist){
		
		OSAdmin::alert("error",ErrorMessage::STOCK_CONFLICT);
	}else if($tb_id==""){
		
		OSAdmin::alert("error",ErrorMessage::NEED_PARAM);
	}else{
		$input_data = array ('tb_id' => $tb_id, 'tb_date' => $tb_date, 'tb_type' => $tb_type, 'tb_provide_name' => $tb_provide_name, 
		    'tb_provide_id' => $tb_provide_id, 'tb_order_id' => $tb_order_id, 'tb_cost_photo' => $sqlFile, 'tb_remark' => $tb_remark);
		$tb_id = TravelBill::addTravelBillInfo ( $input_data );
		if ($tb_id ==0) {
			SysLog::addLog ( UserSession::getUserName(), 'ADD', 'tb_id' ,$tb_id, json_encode($input_data) );
			Common::exitWithSuccess ('账号添加成功','panel/travel_bill.php');
		}else{
			OSAdmin::alert("error");
		}
	}
}

// 获取旅游客户id
$tb_id_post = TravelBill::getTravelBillMaxId();
Template::assign ( 'type_list', $baseConf["optionList"] );
Template::assign("_POST" ,$_POST);
Template::assign("tb_id_posts" ,$tb_id_post);
Template::display ( 'panel/travel_bill_add.tpl' );